﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RSPJ.Struct;
using System.IO;
using System.Text.RegularExpressions;

namespace RSPJ.Function
{
    public static class ReadFile
    {
        public static ROIFile ROI(string P)
        {
            ROIFile roi = new ROIFile();
            //try
            //{
            StreamReader sr = new StreamReader(P);
            string line; ROIPoint point = new ROIPoint();
            int flag = 0, flag1 = 0;

            while (!sr.EndOfStream)
            {
                line = sr.ReadLine();
                if (line != "")
                {
                    if (flag == roi.num) flag = 0;
                    if (roi.list != null)
                        if (roi.list[flag] != null)
                            if (flag1 == roi.list[flag].npts && flag1 != 0)
                            {
                                flag1 = 0;
                                flag++;
                            }

                    if (line.Contains(';'))
                    {
                        if (line.Contains("Number of ROIs:"))
                        {
                            string[] tmp = line.Split(' ');
                            roi.num = Int32.Parse(tmp[4]);
                            roi.list = new ROI[roi.num];
                            continue;
                        }
                        if (line.Contains("File Dimension:"))
                        {
                            string[] tmp = line.Split(' ');
                            roi.lines = Int32.Parse(tmp[3]);
                            roi.samples = Int32.Parse(tmp[5]);
                            continue;
                        }
                        if (line.Contains("ROI name:"))
                        {
                            string[] tmp = line.Split(' ');
                            roi.list[flag] = new ROI();
                            roi.list[flag].name = tmp[3];
                            continue;
                        }
                        if (line.Contains("ROI rgb value:"))
                        {
                            string[] tmp = line.Split(' ');
                            string r = Regex.Replace(tmp[4], @"[^\d.\d]", "");
                            string g = Regex.Replace(tmp[5], @"[^\d.\d]", "");
                            string b = Regex.Replace(tmp[6], @"[^\d.\d]", "");
                            roi.list[flag].r = Int32.Parse(r);
                            roi.list[flag].g = Int32.Parse(g);
                            roi.list[flag].b = Int32.Parse(b);
                            continue;
                        }
                        if (line.Contains("ROI npts:"))
                        {
                            string[] tmp = line.Split(' ');
                            roi.list[flag].npts = Int32.Parse(tmp[3]);
                            flag++;
                            continue;
                        }
                        continue;
                    }

                    string[] ptmp = line.Split(' ');
                    List<string> pl = new List<string>();
                    for (int i = 0; i < ptmp.Length; i++)
                    {
                        if (ptmp[i] != "")
                        {
                            pl.Add(ptmp[i]);
                        }
                    }
                    point = new ROIPoint();
                    point.id = Int32.Parse(pl[0]);
                    point.x = Int32.Parse(pl[1]);
                    point.y = Int32.Parse(pl[2]);
                    point.mapx = Double.Parse(pl[3]);
                    point.mapy = Double.Parse(pl[4]);
                    for(int i=5;i<pl.Count;i++)
                    {
                        point.b.Add(Int32.Parse(pl[i]));
                    }
                    roi.list[flag].list.Add(point);
                    flag1++;
                }
            }
            //}
            //catch
            //{
            //    MessageBox.Show("Error!");
            //}
            return roi;
        }
    }
}
